# Makefile for BMP Histogram Generator
# 
# 用于编译BMP图像直方图生成器的Makefile

# 编译器设置
CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -O2 -g
LDFLAGS = -lm

# 目标文件和源文件
TARGET = bar_chart
SRCDIR = .
OBJDIR = obj
SOURCES = $(wildcard $(SRCDIR)/*.c)
OBJECTS = $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)

# 默认目标
.PHONY: all
all: $(TARGET)

# 创建目标目录
$(OBJDIR):
	mkdir -p $(OBJDIR)

# 编译目标程序
$(TARGET): $(OBJECTS)
	$(CC) $(OBJECTS) -o $@ $(LDFLAGS)
	@echo "编译完成: $(TARGET)"

# 编译目标文件
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR)
	$(CC) $(CFLAGS) -c $< -o $@

# 清理编译产物
.PHONY: clean
clean:
	rm -rf $(OBJDIR) $(TARGET) $(TARGET).exe
	@echo "清理完成"

# 安装（可选）
.PHONY: install
install: $(TARGET)
	@echo "安装到 /usr/local/bin (需要sudo权限):"
	sudo cp $(TARGET) /usr/local/bin/

# 运行测试（需要测试图片）
.PHONY: test
test: $(TARGET)
	@if [ -f "test.bmp" ]; then \
		echo "运行测试..."; \
		./$(TARGET) test.bmp -s 30; \
	else \
		echo "错误: 需要test.bmp文件进行测试"; \
	fi

# 调试编译
.PHONY: debug
debug: CFLAGS += -DDEBUG -g3 -O0
debug: $(TARGET)
	@echo "调试版本编译完成"

# 发布编译
.PHONY: release
release: CFLAGS += -DNDEBUG -O3
release: clean $(TARGET)
	strip $(TARGET)
	@echo "发布版本编译完成"

# 显示帮助信息
.PHONY: help
help:
	@echo "可用的make目标:"
	@echo "  all      - 编译程序 (默认)"
	@echo "  clean    - 清理编译产物"
	@echo "  debug    - 编译调试版本"
	@echo "  release  - 编译发布版本"
	@echo "  test     - 运行测试 (需要test.bmp)"
	@echo "  install  - 安装到系统"
	@echo "  help     - 显示此帮助信息"

# 显示编译信息
.PHONY: info
info:
	@echo "编译器: $(CC)"
	@echo "编译选项: $(CFLAGS)"
	@echo "链接选项: $(LDFLAGS)"
	@echo "源文件: $(SOURCES)"
	@echo "目标文件: $(OBJECTS)"
	@echo "目标程序: $(TARGET)"